package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Created by mz on 14/9/17.
 *
 */
@Data
public class RetailImmunizationSubRecord implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -1706547146889747443L;

    /**
     *
     */
    private Long id;

    /**
     * 主表id
     */
    private Long vaccineId;

    /**
     * 二级畜种
     */
    private Integer breedSecondType;

    /**
     * 免疫类型
     */
    private Integer vaccineType;

    /**
     * 其他类型
     */
    private String otherType;

    /**
     * 疫苗类型code
     */
    private Integer vaccineCode;

    /**
     * 疫苗类型name
     */
    private String vaccineName;

    /**
     *
     */
    private Date date;

    /**
     * 天数
     */
    private Integer days;

    /**
     * 期次
     */
    private Integer times;

    /**
     * 数量
     */
    private Integer quantity;

    /**
     * 用药量
     */
    private Double drugQuantity;

    /**
     * 用药方法
     */
    private Integer method;

    /**
     * 药品单位
     */
    private Integer drugUnit;

    /**
     * 生产厂家照片
     */
    @JsonIgnore
    private String manufacturerJson;

    /**
     *
     */
    private Map<String, Map<String , Object>> manufacturer;

    /**
     * 批次照片
     */
    @JsonIgnore
    private String batchJson;

    /**
     *
     */
    private Map<String, Map<String , Object>> batch;

    /**
     *'免疫操作人员
     */
    private String  operatorName;

    /**
     * 单据是否有效
     */
    private Integer deleteFlag;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;


    public void setManufacturerJson(String manufacturerJson){
        this.manufacturerJson = manufacturerJson;

        if(Strings.isNullOrEmpty(manufacturerJson)){
            this.manufacturer = Maps.newHashMap();
        }else {
            try {
                this.manufacturer =
                        objectMapper.readValue(manufacturerJson, new TypeReference<Map<String,Map<String,Object>>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }


    public void setManufacturer(Map<String,Map<String,Object>> manufacturer){
        this.manufacturer = manufacturer;

        if(manufacturer == null || manufacturer.isEmpty()){
            this.manufacturerJson = null;
        }else {
            try {
                this.manufacturerJson =
                        objectMapper.writeValueAsString(manufacturer);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }


    public void setBatchJson(String batchJson){
        this.batchJson = batchJson;

        if(Strings.isNullOrEmpty(batchJson)){
            this.batch = Maps.newHashMap();
        }else {
            try {
                this.batch =
                        objectMapper.readValue(batchJson, new TypeReference<Map<String,Map<String,Object>>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }


    public void setBatch(Map<String,Map<String,Object>> batch){
        this.batch = batch;

        if(batch == null || batch.isEmpty()){
            this.batchJson = null;
        }else {
            try {
                this.batchJson =
                        objectMapper.writeValueAsString(batch);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }
}
